﻿using System;
using StockholmsStad.Web.UI.Class.Business;

namespace StockholmsStad.Web.UI.UIServiceReference
{
    /// <summary>
    /// Påbyggnad på servicelagrets kontrakt för person.
    /// </summary>
    public partial class Person
    {
        private static readonly string[] SWEDISH_NAME = new[] {"Svenskt", "Svensk"};

        #region Properties

        /// <summary>
        /// Returnerar personnummret som ett affärsobjekt
        /// </summary>
        public PersonalNumber PersonalNumberUI
        {
            get { return new PersonalNumber(PersonalNumberField); }
            set
            {
                if (value != null)
                {
                    PersonalNumberField = value.ToString();
                }
            }
        }

        #endregion

        /// <summary>
        /// Kontruktor för att skapa en fullständig person
        /// </summary>
        /// <param name="surNames">Förnamn och mellannamn</param>
        /// <param name="lastName"></param>
        /// <param name="personalNumber"></param>
        /// <param name="swedish"></param>
        /// <param name="nationality"></param>
        /// <param name="sexMale"></param>
        /// <param name="postAddress">Address, kan vara null</param>
        public Person(string surNames, string lastName, PersonalNumber personalNumber, bool swedish, string nationality,
                      bool sexMale, Address postAddress)
        {
            FirstNames = surNames;
            LastName = lastName;
            PersonalNumberUI = personalNumber;
            if (swedish || IsSwedish(nationality))
            {
                Nationality = SWEDISH_NAME[0];
                SwedishNationality = true;
            }
            else
            {
                Nationality = nationality;
                SwedishNationality = false;
            }
            Sex = sexMale ? UIServiceReference.Sex.Male : UIServiceReference.Sex.Female;
            Address = postAddress;
        }

        /// <summary>
        /// Returnerar true om nationaliteten är svensk
        /// </summary>
        /// <param name="nationality"></param>
        /// <returns></returns>
        public static bool IsSwedish(string nationality)
        {
            if (nationality == null)
                return false;
            foreach (string name in SWEDISH_NAME)
            {
                if (nationality.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                    return true;
            }
            return false;
        }
    }
}